package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.bean.IdCodeBean;
import com.dingwei.lesyndrome.mvp.presenter.RegisterPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.KeyboardUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/24.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 注册
 */

public class RegisterActivity extends BaseActivity implements Contract.RegisterView {

    RegisterPresenter registerPresenter = new RegisterPresenter(this);
    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.et_account_number)
    EditText etAccountNumber;
    @Bind(R.id.et_code)
    EditText etCode;
    @Bind(R.id.tv_get_code)
    TextView tvGetCode;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.btn_register)
    SuperButton btnRegister;
    @Bind(R.id.m_checkbox)
    CheckBox mCheckbox;
    @Bind(R.id.ll_root)
    LinearLayout llRoot;
    @Bind(R.id.tv_agreement)
    TextView tvAgreement;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        KeyboardUtils.autoScrollView(llRoot, btnRegister);

        etAccountNumber.addTextChangedListener(textWatcher);
        etCode.addTextChangedListener(textWatcher);
        etPassword.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(etAccountNumber.getText().toString()) && !TextUtils.isEmpty(etCode.getText().toString()) && !TextUtils.isEmpty(etPassword.getText().toString())) {
                btnRegister.setShapeSolidColor(getResources().getColor(R.color.blue)).setUseShape();
                btnRegister.setEnabled(true);
            } else {
                btnRegister.setShapeSolidColor(getResources().getColor(R.color.button_no_clickable_color)).setUseShape();
                btnRegister.setEnabled(false);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_login, R.id.tv_get_code, R.id.btn_register,R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                intent = new Intent(BaseActivity.context, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.tv_get_code:
                registerPresenter.getImgCode(etAccountNumber.getText().toString());
                break;
            case R.id.btn_register:
                if (mCheckbox.isChecked()) {
                    registerPresenter.register(etAccountNumber.getText().toString(), etCode.getText().toString(), etPassword.getText().toString());
                } else {
                    showToast("请同意用户注册协议");
                }
                break;
            case R.id.tv_agreement:
                intent = new Intent(BaseActivity.context,AgreementActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void CountDownTimerFinish() {
        tvGetCode.setTextColor(getResources().getColor(R.color.blue));
        tvGetCode.setText("获取验证码");
        tvGetCode.setEnabled(true);
    }

    @Override
    public void CountDownTimerOnTick(long millisUntilFinished) {
        tvGetCode.setTextColor(getResources().getColor(R.color.text_color_gray));
        tvGetCode.setText("请等待" + millisUntilFinished / 1000 + "秒");
        tvGetCode.setEnabled(false);
    }

    @Override
    public void getCodeSuccess(IdCodeBean bean) {
        etCode.setText(bean.data);
    }

    @Override
    public void registerSuccess() {
        showToast("注册成功");
        finish();
    }


}
